package com.demo.api.controller.api.menu;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.api.cache.MenuCache;
import com.demo.api.controller.BaseController;
import com.demo.api.service.menu.IMenuService;
import com.demo.api.vo.user.MenuVo;
import com.demo.core.annotation.AuthToken;
import com.demo.core.response.ApiResponse;
import com.demo.core.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tan-Bowen
 * @version 1.0
 * @description TODO
 * @date 2021/7/27 14:52
 */

@Api(value = "菜单管理", tags = "菜单管理" )
@Slf4j
@RestController
@RequestMapping("/api/menu")
public class MenuController extends BaseController {
    @Resource
    private IMenuService menuService;

    @Resource
    private MenuCache menuCache;

    @AuthToken(checkPower = false)
    @PostMapping("/page")
    @ApiOperation(value = "菜单分页列表查询")
    public ApiResponse<IPage<MenuVo.PageList>> menuPage(PageUtil.Pages page, MenuVo.Select select){
        return ApiResponse.success(menuService.pageList(getPage(page),select));
    }

    @AuthToken(checkPower = false)
    @GetMapping("/all")
    @ApiOperation(value = "所有菜单")
    public ApiResponse<List<MenuVo.PageList>> menuAll(){
        return ApiResponse.success(menuService.getMenuList());
    }

    //  后面修改为当前用户的菜单和权限 // 目前获取的是所有
    @AuthToken(checkPower = false)
    @GetMapping("/menuAndAuth")
    @ApiOperation(value = "所有菜单和权限")
    public ApiResponse<MenuVo.MenuAuthMap> menuAndAuth(){
        return ApiResponse.success(menuCache.getMenuAuthMap());
    }

    @AuthToken(checkPower = false)
    @PostMapping("/create")
    @ApiOperation(value = "菜单新增")
    public ApiResponse<Void> create(@Validated MenuVo.Create request){
        menuService.create(request);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/update")
    @ApiOperation(value = "菜单编辑")
    public ApiResponse<Void> update(@Validated MenuVo.Update update){
        menuService.update(update);
        return ApiResponse.success();
    }


    @AuthToken(checkPower = false)
    @PostMapping("/deleteMenu")
    @ApiOperation(value = "菜单删除")
    public ApiResponse<Void> deleteMenu(@RequestParam("id")Long id){
        menuService.deleteMenu(id);
        return ApiResponse.success();
    }
}